package com.bjpowernode.crm.settings.service.impl;

import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.mapper.DicValueMapper;
import com.bjpowernode.crm.settings.service.DicValueService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * ClassName: DicValueServiceImpl<br/>
 * Package: com.bjpowernode.crm.settings.service.impl<br/>
 * Description: <br/>
 * Date: 2022/7/4 7:44<br/>
 * Auther:对的我写的，错的我同事写的<br/>
 */
@Service
public class DicValueServiceImpl implements DicValueService {

    @Resource
    private DicValueMapper dicValueMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<DicValue> getDicValues() {
        return dicValueMapper.getDicValues();
    }

    @Override
    public Integer insertDicVal(DicValue dicValue) {
        int count = dicValueMapper.insert(dicValue);
        if(count !=1)
        {
            count=0;
        }
        redisTemplate.delete(dicValue.getTypeCode());
        return count;
    }

    @Override
    public DicValue getDicValueById(String id) {
        return dicValueMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateDicVal(DicValue dicValue) {
        int count = dicValueMapper.updateByPrimaryKey(dicValue);
        if(count !=1)
        {
            count=0;
        }
        redisTemplate.delete(dicValue.getTypeCode());
        return count;
    }

    @Override
    public Integer delDicValsByIds(String[] ids) {
        String[] idsData=new String[ids.length];
        Set typeCodeData=new HashSet();
        for(int i=0;i<ids.length;i++)
        {
           String[] idsStr = ids[i].split("-");
           idsData[i]=idsStr[0];
           typeCodeData.add(idsStr[1]);
        }

        int count =  dicValueMapper.delDicValsByIds(idsData);
        if(count !=idsData.length)
        {
            count=0;
        }else {
         redisTemplate.delete(typeCodeData);
        }
        return count;
    }
}
